import {getFaultList, getFault} from '@/base/api/ServiceApi';

export class FaultSelectorBloc {
  value: Array<any>=[];
  options: Array<any>=[];
  orginalOptions: Array<any>=[];
  normalizedOptions:Array<any>=[];
  normalizeOptions(data) {
    return data.map(item => ({
      value: item.FaultHugeID,
      label: item.FaultHugeName,
      children: this.normalizeOptions(item.children).map(subItem => ({
        value: subItem.FaultID,
        label: subItem.FaultName,
        FaultDescription: subItem.FaultDescription
      }))
    }));
  }
  load() {
    return getFaultList()
      .then((res) => {
        // @ts-ignore
        let json = JSON.parse(res.data
          .replace(/FaultHugeID/g, 'value')
          .replace(/FaultHugeName/g, 'label')
          .replace(/FaultID/g, 'value')
          .replace(/FaultName/g, 'label')
        );
        this.normalizedOptions = json.errMsg.items;
        return res;
      })
  }
  getFault(id:number) {
    return getFault({id}).then((res) => {
      // @ts-ignore
      let faultJson = JSON.parse(res.data);
      if (faultJson.errMsg.items) {
        this.value = faultJson.errMsg.items;
        console.log(this.value);
      }
    })
  }
}
